#include "map.h"

int createTextureFromSubSurface(SDL_Renderer* renderer, SDL_Surface* imageSurface, SDL_Rect cutRect, SDL_Texture** imageTexture) {

     // 创建指定尺寸
    SDL_Surface* subSurface = SDL_CreateRGBSurface(0, BLOCK_WIDTH, BLOCK_HEIGHT, imageSurface->format->BitsPerPixel,
                                                       imageSurface->format->Rmask, imageSurface->format->Gmask,
                                                       imageSurface->format->Bmask, imageSurface->format->Amask);
    if (subSurface == nullptr) {
        SDL_Log("Failed to create surface: %s", SDL_GetError());
        return -1;
    }

    // 复制一部分到新的表面(素材块的比例是32 * 32)
    SDL_BlitSurface(imageSurface, &cutRect, subSurface, nullptr);

    // 创建纹理
    *imageTexture = SDL_CreateTextureFromSurface(renderer, subSurface);
    if (imageTexture == nullptr) {
        SDL_Log("Failed to create texture: %s", SDL_GetError());
        SDL_FreeSurface(subSurface);
        return -2;
    }

    return 0;
}

// g++ number.cpp -lSDL2 -lSDL2_image
int main(int argc, char* argv[]) {

    int WINDOW_WIDTH = X_BLOCK_NUM * BLOCK_WIDTH;
    int WINDOW_HEIGHT = Y_BLOCK_NUM * BLOCK_HEIGHT;

    int imgFlags = IMG_INIT_PNG;

    SDL_Window* window = nullptr;
    SDL_Renderer* renderer = nullptr;
    SDL_Surface *tilesetsSurface = nullptr;
    SDL_Texture *imageTexture = nullptr;
    SDL_Texture *number0 = nullptr;
    // 0 黄钥匙
    SDL_Rect yellowKeyPostion = { 0, 0, 0, 0 };
    SDL_Rect destKeyPostion = { 0, 0, 0, 0 };

    // 事件循环
    SDL_Event event;
    bool quit = false;

    static const int MIN_FRAME_TIME = 16;
    Uint32 frameTime = 0;

    // 初始化SDL
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
        return 1;
    }

    // 创建窗口
    window = SDL_CreateWindow("Magic Tower", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == nullptr) {

        SDL_Log("Failed to create window: %s", SDL_GetError());
        goto end;
    }

    // 创建渲染器
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == nullptr) {
        SDL_Log("Failed to create renderer: %s", SDL_GetError());
        goto end;
    }

    // 初始化SDL_image
    if (!(IMG_Init(imgFlags) & imgFlags)) {
        SDL_Log("Failed to initialize SDL_image: %s", IMG_GetError());
        goto end;
    }

    tilesetsSurface = IMG_Load("tiled/number.png");
    if (tilesetsSurface == nullptr) {

        SDL_Log("Failed to load map.png: %s", IMG_GetError());
        return 2;
    }

    printf("%d %d\n", tilesetsSurface->w, tilesetsSurface->h);
    yellowKeyPostion.x = 5 * (tilesetsSurface->w / 10);
    yellowKeyPostion.h = tilesetsSurface->h;
    yellowKeyPostion.w = tilesetsSurface->w / 10;

    destKeyPostion.h = tilesetsSurface->h;
    destKeyPostion.w = tilesetsSurface->w / 10;

    // imageTexture = SDL_CreateTextureFromSurface(renderer, tilesetsSurface);
    // if (!imageTexture) {

    //     SDL_Log("SDL_CreateTextureFromSurface %s", SDL_GetError());
    // }

    if (createTextureFromSubSurface(renderer, tilesetsSurface, yellowKeyPostion, &number0) < 0) {

    }

    imageTexture =  SDL_CreateTextureFromSurface(renderer, tilesetsSurface);
    if (imageTexture == nullptr) {
        SDL_Log("Failed to create texture: %s", SDL_GetError());
        return -2;
    }

    while (!quit) {

        SDL_RenderClear(renderer);

        // 背景填充颜色
        // SDL_SetRenderDrawColor(renderer, 93, 148, 252, 255);
        // SDL_RenderClear(renderer);

        frameTime = SDL_GetTicks();
        while (SDL_PollEvent(&event)) {

            if (event.type == SDL_QUIT) {
                quit = true;
            } else if (event.type == SDL_KEYDOWN) {

        
            }
        }

        
        SDL_RenderCopy(renderer, number0, NULL, &destKeyPostion);

        // 显示固定图层
        SDL_RenderPresent(renderer);

        if(SDL_GetTicks() - frameTime < MIN_FRAME_TIME) {
			SDL_Delay(MIN_FRAME_TIME - (SDL_GetTicks () - frameTime));
		}
    }

    // 清理资源
    // SDL_DestroyTexture(imageTexture);
    // SDL_DestroyTexture(imageTexture1);
    // SDL_FreeSurface(imageSurface);
    // IMG_Quit();
    // SDL_DestroyRenderer(renderer);
    // SDL_DestroyWindow(window);
    // SDL_Quit();

end:

    if (window)
        SDL_DestroyWindow(window);
    
    if (renderer)
        SDL_DestroyRenderer(renderer);

    SDL_Quit();

    return 0;
}